package vn.edu.saigontech.ncms.service.impl;

import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import vn.edu.saigontech.ncms.model.info.Student;
import vn.edu.saigontech.ncms.model.info.StudentType;
import vn.edu.saigontech.ncms.model.program.Level;
import vn.edu.saigontech.ncms.model.program.Program;
import vn.edu.saigontech.ncms.model.result.MoveUpTest;
import vn.edu.saigontech.ncms.model.user.User;
import vn.edu.saigontech.ncms.service.MoveUpTestService;
import vn.edu.saigontech.ncms.util.Checker;

@SuppressWarnings("unchecked")
public class MoveUpTestServiceImpl extends
		HibernateGenericService<MoveUpTest, Long> implements MoveUpTestService {

	@Override
	public MoveUpTest createBlankMoveUpTest() {
		Program program = new Program();
		Level level = new Level();
		level.setProgram(program);
		Student student = new Student();
		student.setType(new StudentType());
		student.setConsultant(new User());
		student.setProgram(new Program());
		MoveUpTest moveUpTest = new MoveUpTest();
		moveUpTest.setStudent(student);
		moveUpTest.setLevel(level);
		return moveUpTest;
	}

	@Override
	public Collection<MoveUpTest> find(MoveUpTest moveUpTest) {
		Criteria criteria = this.createCriteria();
		Criteria studentCriteria = criteria.createCriteria("student");

		if (!Checker.isEmpty(moveUpTest.getStudent().getFirstName())) {
			studentCriteria.add(Restrictions.ilike("firstName", moveUpTest
					.getStudent().getFirstName(), MatchMode.ANYWHERE));
		}
		if (!Checker.isEmpty(moveUpTest.getStudent().getLastName())) {
			studentCriteria.add(Restrictions.ilike("lastName", moveUpTest
					.getStudent().getLastName(), MatchMode.ANYWHERE));
		}
		if (!Checker.isNull(moveUpTest.getFromTestDate())
				&& !Checker.isNull(moveUpTest.getToTestDate())) {
			criteria.add(Restrictions.between("date", moveUpTest
					.getFromTestDate(), moveUpTest.getToTestDate()));
		} else if (!Checker.isNull(moveUpTest.getDate())) {
			criteria.add(Restrictions.eq("date", moveUpTest.getDate()));
		}
		if (!Checker.isNull(moveUpTest.getStudent().getType().getId())) {
			studentCriteria.createCriteria("type").add(
					Restrictions.eq("id", moveUpTest.getStudent().getType()
							.getId()));
		}
		if (!Checker.isNull(moveUpTest.getLevel().getId())) {
			criteria.createCriteria("level").add(
					Restrictions.eq("id", moveUpTest.getLevel().getId()));
		}
		if (!Checker.isNull(moveUpTest.getStudent().getFromConsultedDate())
				&& !Checker.isNull(moveUpTest.getStudent()
						.getToConsultedDate())) {
			studentCriteria.add(Restrictions.between("consultedDate",
					moveUpTest.getStudent().getFromConsultedDate(),
					moveUpTest.getStudent().getToConsultedDate()));
		} else if (!Checker.isNull(moveUpTest.getStudent()
				.getConsultedDate())) {
			studentCriteria.add(Restrictions.eq("consultedDate", moveUpTest
					.getStudent().getConsultedDate()));
		}
		if (!Checker.isNull(moveUpTest.getStudent().getProgram().getId())) {
			studentCriteria.createCriteria("program").add(
					Restrictions.eq("id", moveUpTest.getStudent()
							.getProgram().getId()));
		}
		if (!Checker.isNull(moveUpTest.getStudent().getConsultant().getId())) {
			studentCriteria.createCriteria("consultant").add(
					Restrictions.eq("id", moveUpTest.getStudent()
							.getConsultant().getId()));
		}
		studentCriteria.addOrder(Order.asc("firstName"));
		return criteria.list();
	}
}